package server.boot;

import java.io.FileInputStream;

/**
 * @author: chenhs
 * @date: Created in 7:06 2019/12/22
 **/
public class MyClassLoader extends ClassLoader {


    private String classPath;

    public MyClassLoader(String classPath) {
        this.classPath = classPath;
    }

    private byte[] loadByte() throws Exception {
        FileInputStream fis = new FileInputStream(classPath);
        int len = fis.available();
        byte[] data = new byte[len];
        fis.read(data);
        fis.close();

        return data;
    }

    /**
     * @param name
     * @return
     * @throws ClassNotFoundException
     */
    @Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            byte[] data = loadByte();
            return defineClass(name, data, 0, data.length);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ClassNotFoundException();
        }
    }

}
